package com.ksv.qlsv;

import com.ksv.lop.*;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class SinhVienEditActivity extends Activity {
	private TextView txt_masv;
	private TextView txt_hosv;
	private Button btn_save;
	private Button btn_cancel;
	private TextView txt_tensv;
	private CheckBox ch_gioitinh;
	private TextView txt_diachi;
	private Spinner com_lop;
	
	public static SinhVien sv = new SinhVien();
	public static int iAddEditView = 1;
	public static boolean bSave = false;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialogedit_sv);            
        this.setTitle(getResources().getString(R.string.sinhvien));
        btn_save = (Button)findViewById(R.id.btn_luu);
        btn_save.setText(getResources().getString(R.string.luu));
		btn_cancel = (Button)findViewById(R.id.btn_huy);
		btn_cancel.setText(getResources().getString(R.string.huy));
        txt_masv = (TextView)findViewById(R.id.txt_masv);
        txt_hosv = (TextView)findViewById(R.id.txt_hosv);    
        txt_tensv = (TextView)findViewById(R.id.txt_tensv);    
        ch_gioitinh = (CheckBox)findViewById(R.id.ch_checked);    
        txt_diachi = (TextView)findViewById(R.id.txt_diachi);    
        com_lop = (Spinner)findViewById(R.id.com_lop);
        sinhVienEnableTrueAll();
        loadListLop();
        if(iAddEditView == 1){
        	sv.setMaSV(ConnectDB.getSinhVienMax());
        	txt_masv.setText(String.valueOf(sv.getMaSV()));
        	txt_masv.setEnabled(false);
        	txt_hosv.setHint(getResources().getString(R.string.hosv));
        	txt_tensv.setHint(getResources().getString(R.string.tensv));
        	txt_diachi.setHint(getResources().getString(R.string.diachi));
        }
        else if(iAddEditView == 2){
        	txt_masv.setText(String.valueOf(sv.getMaSV()));        
        	txt_masv.setEnabled(false);
        	txt_hosv.setText(sv.getHoSV());
        	txt_tensv.setText(sv.getTenSV());
        	ch_gioitinh.setChecked(sv.getGioiTinh());
        	txt_diachi.setText(sv.getDiaChi());
        	com_lop.setSelection(getPosiontionListLop());
        }
        else{
        	txt_masv.setText(String.valueOf(sv.getMaSV()));        
        	sinhVienEnableFalseAll();
        	txt_hosv.setText(sv.getHoSV());
        	txt_tensv.setText(sv.getTenSV());
        	ch_gioitinh.setChecked(sv.getGioiTinh());
        	txt_diachi.setText(sv.getDiaChi());
        	com_lop.setSelection(getPosiontionListLop());
        }
        onCheckedChanged();
        onItemSelestedChanged();
        onClickButtonSave();
        onClickButtonCancel();        
    }

	private void onItemSelestedChanged() {
		com_lop.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View view,
					int position, long id) {
				sv.setMaLop((int)id);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private void onCheckedChanged() {
		ch_gioitinh.setOnCheckedChangeListener(new OnCheckedChangeListener() {			
			@Override
			public void onCheckedChanged(CompoundButton checkBox, boolean check) {
				sv.setGioiTinh(check);
				if(check == true)
					checkBox.setText(getResources().getString(R.string.nam));
				else
					checkBox.setText(getResources().getString(R.string.nu));
			}
		});
	}

	private void sinhVienEnableTrueAll() {
		txt_masv.setEnabled(true);
        txt_hosv.setEnabled(true);
        txt_tensv.setEnabled(true);
        ch_gioitinh.setEnabled(true);
        txt_diachi.setEnabled(true);
        com_lop.setEnabled(true);
	}
	
	private void sinhVienEnableFalseAll() {
		txt_masv.setEnabled(false);
        txt_hosv.setEnabled(false);
        txt_tensv.setEnabled(false);
        ch_gioitinh.setEnabled(false);
        txt_diachi.setEnabled(false);
        com_lop.setEnabled(false);
	}
	
	private void loadListLop() {
		try{		    
			com_lop.setAdapter(null);		       
    	    Cursor c = ConnectDB.getAllLopHoc();
    	    KhoaAdapter.mID = ConnectDB.malop;
    	    KhoaAdapter.mName = ConnectDB.tenlop;
    	    KhoaAdapter khoaAdapter = new KhoaAdapter(this, c);
    	    com_lop.setAdapter(khoaAdapter);
    	}
    	catch(Exception ex){
    		
    	}
	}
	
	private int getPosiontionListLop() {
		for (int i = 0; i < com_lop.getCount(); i++) {
			try{
				Cursor c = (Cursor) com_lop.getItemAtPosition(i);
				if(c.getInt(c.getColumnIndex(ConnectDB.malop)) == sv.getMaLop()){
					return i;
				}
			}
			catch(Exception Ex){
				
			}
		}
		return 0;
	}

	private void onClickButtonSave() {
		// TODO Auto-generated method stub		
		btn_save.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				bSave = true;
				if(iAddEditView == 1){
					sv.setHoSV(txt_hosv.getText().toString());
					sv.setTenSV(txt_tensv.getText().toString());
					sv.setDiaChi(txt_diachi.getText().toString());
					sv.setMaLop((int)com_lop.getSelectedItemId());
					sv.setGioiTinh(ch_gioitinh.isChecked());
					ConnectDB.AddSinhVien(sv);
					SinhVienEditActivity.this.finish();
				}
				else if(iAddEditView == 2){
					sv.setHoSV(txt_hosv.getText().toString());
					sv.setTenSV(txt_tensv.getText().toString());
					sv.setDiaChi(txt_diachi.getText().toString());
					sv.setMaLop((int)com_lop.getSelectedItemId());
					ConnectDB.updateSinhVien(sv);
					SinhVienEditActivity.this.finish();
				}
				else{
					SinhVienEditActivity.this.finish();
				}
			}
		});
	}

	private void onClickButtonCancel() {
		// TODO Auto-generated method stub
		btn_cancel.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub	
				bSave = false;
				SinhVienEditActivity.this.finish();
			}
		});		
	}
	
	
}
